package com.hxzy.controller.front.dictdata;

import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.dictdata.resp.DictDataResp;
import com.hxzy.controller.admin.mapstruct.DictDataConvert;
import com.hxzy.entity.SysDictData;
import com.hxzy.util.DictCacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/12 9:47
 * @Version 1.0
 */
@Api(tags = "前端-数据字典明细")
@RestController
@RequestMapping(value = "/front/dictdata")
public class FrontDictDataController {

    /**
     * redis缓存字典
     */
    @Autowired
    private DictCacheUtil dictCacheUtil;


    @ApiOperation(value = "根据dict_type查询redis缓存中的值")
    @GetMapping(value = "/bynames")
    public R<Map<String, List<DictDataResp>>> bynames(String[] dicts){
        if(Objects.isNull(dicts) || dicts.length==0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        Map<String,List<DictDataResp>> dataMap=new HashMap<>();
        for(String key : dicts){
            List<SysDictData> byDictType = this.dictCacheUtil.findByDictType(key);
            //有缓存
            if(Objects.nonNull(byDictType)){
                dataMap.put(key, DictDataConvert.INSTANCE.convert(byDictType));
            }
        }
        return R.okHasData(dataMap);
    }

}
